# # Compiler and compiler flags
# CXX = g++
# CXXFLAGS = -std=c++17 -Wall -Wextra -g # Using C++17, all warnings, extra warnings, and debug symbols

# # Directories
# IDIRS = -I./CS -I./include -I./third_party # Include directories for headers
# SRCDIR = src
# OBJDIR = obj
# BINDIR = bin

# # Source files: find all .cpp files in SRCDIR
# SRCS = $(wildcard $(SRCDIR)/*.cpp)

# # Object files: replace .cpp with .o and put them in OBJDIR
# OBJS = $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS))

# # Executable name
# TARGET = $(BINDIR)/ECA

# # Default target: build all
# all: $(TARGET)

# # Rule to link the executable
# # Rule to link the executable
# $(TARGET): $(OBJS)
# 	@mkdir -p $(BINDIR)
# 	$(CXX) $(CXXFLAGS) -o $@ $^

# # Rule to compile source files into object files
# $(OBJDIR)/%.o: $(SRCDIR)/%.cpp | $(OBJDIR)
# 	$(CXX) $(CXXFLAGS) $(IDIRS) -c $< -o $@

# # Rule to create the object directory
# $(OBJDIR):
# 	@mkdir -p $(OBJDIR)

# # Clean up build files
# clean:
# 	rm -rf $(OBJDIR) $(TARGET)

# # Phony targets: targets that don't represent actual files
# .PHONY: all clean

# Compiler and compiler flags
CXX = g++
CXXFLAGS = -std=c++17 -Wall -Wextra -g # Using C++17, all warnings, extra warnings, and debug symbols

# Directories
IDIRS = -I./include -I./third_party # Common include directories
SRCDIR_SERVER = src
SRCDIR_CLIENT = CS
OBJDIR = obj
BINDIR = bin

# --- SERVER TARGET ---
# Source files for the server
SERVER_SRCS = $(wildcard $(SRCDIR_SERVER)/*.cpp)
# Object files for the server
SERVER_OBJS = $(patsubst $(SRCDIR_SERVER)/%.cpp,$(OBJDIR)/%.o,$(SERVER_SRCS))
# Executable name for the server
SERVER_TARGET = $(BINDIR)/server

# --- CLIENT TARGET ---
# Source file for the client
CLIENT_SRCS = $(wildcard $(SRCDIR_CLIENT)/client_main.cpp)
# Object file for the client
CLIENT_OBJS = $(patsubst $(SRCDIR_CLIENT)/%.cpp,$(OBJDIR)/%.o,$(CLIENT_SRCS))
# Executable name for the client
CLIENT_TARGET = $(BINDIR)/client


# Default target: build all executables
all: $(SERVER_TARGET) $(CLIENT_TARGET)

# Rule to link the SERVER executable
$(SERVER_TARGET): $(SERVER_OBJS)
	@mkdir -p $(BINDIR)
	$(CXX) $(CXXFLAGS) -o $@ $^ -lpthread # Add -lpthread for std::thread

# Rule to link the CLIENT executable
$(CLIENT_TARGET): $(CLIENT_OBJS)
	@mkdir -p $(BINDIR)
	$(CXX) $(CXXFLAGS) -o $@ $^

# Generic rule to compile source files from ANY source dir into object files
$(OBJDIR)/%.o: $(SRCDIR_SERVER)/%.cpp | $(OBJDIR)
	$(CXX) $(CXXFLAGS) $(IDIRS) -c $< -o $@

$(OBJDIR)/%.o: $(SRCDIR_CLIENT)/%.cpp | $(OBJDIR)
	$(CXX) $(CXXFLAGS) $(IDIRS) -c $< -o $@

# Rule to create the object directory
$(OBJDIR):
	@mkdir -p $(OBJDIR)

# Clean up build files
clean:
	rm -rf $(OBJDIR) $(BINDIR)

# Phony targets: targets that don't represent actual files
.PHONY: all clean